$(function () {
    findById();
});

function findById() {
    let id = sessionStorage.getItem("productId");
    let result = myAjax("/back/product/findById", {id: id});
    console.log(result);
    if (result != null) {
        setData(result.model);
    }

}

function setData(data) {

    $("#cuisineId option[value='" + data.cuisineId + "']").attr("selected", true);
    $("#productName").val(data.name);
    $("#price").val(data.price);
    $("#normalPrice").val(data.normalPrice);
    $("#showImgHref").attr("src", data.imgHref);
    $("input[name='isShow'][value=" + data.isShow + "]").attr("checked", true);
}

function save() {
    let data = new FormData();
    data.append("cuisineId", $("#cuisineId").val());
    data.append("name", $("#productName").val());
    data.append("price", $("#price").val());
    data.append("isShow", $('input:radio[name="isShow"]:checked').val());
    data.append("normalPrice", $("#normalPrice").val());
    data.append("imgHref", $("#imgHref")[0].files[0]);
    data.append("content", edit.txt.html());
    data.append("id", sessionStorage.getItem("productId"));
    let result = myImgAjax("/back/product/update", data);
    console.log(result);
    if (result.result == "success") {
        load("/product/productManage.html")
    } else {
        alert("新增失败");
    }
}